package fun.yizhierha.operation.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import fun.yizhierha.common.base.BaseErrDto;
import fun.yizhierha.common.exception.BadRequestException;
import fun.yizhierha.common.exception.BizCodeEnum;
import fun.yizhierha.common.utils.PageUtils;
import fun.yizhierha.common.utils.Query;
import fun.yizhierha.common.utils.SecurityUtils;
import fun.yizhierha.common.utils.ValidList;
import fun.yizhierha.common.utils.file.ExcelUtils;
import fun.yizhierha.operation.domain.OraApp;
import fun.yizhierha.operation.domain.vo.CreateOraAppVo;
import fun.yizhierha.operation.domain.vo.UpdateOraAppVo;
import fun.yizhierha.operation.domain.vo.RetrieveOraAppVo;
import fun.yizhierha.operation.mapper.OraAppMapper;
import fun.yizhierha.operation.service.mapstruct.OraAppMapstruct;
import fun.yizhierha.operation.service.OraAppService;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.*;
import javax.servlet.http.HttpServletResponse;

/** generated by EH-Admin
* @author xaopohi
* @date Wed Dec 21 15:15:54 CST 2022
**/
@Service
@RequiredArgsConstructor
public class OraAppServiceImpl extends ServiceImpl<OraAppMapper, OraApp> implements OraAppService{

    private final OraAppMapstruct oraAppMapstruct;

    @Override
    public PageUtils<OraApp> list(RetrieveOraAppVo retrieveOraAppVo, Query.PageVo pageVo) {
        QueryWrapper<OraApp> wrapper = new QueryWrapper<>();
        String name = retrieveOraAppVo.getName();

        if (name != null){
            wrapper.like(OraApp.COL_NAME,name);
        }


        IPage<OraApp> iPage = baseMapper.selectPage(new Query<OraApp>().getPage(pageVo), wrapper);
        return new PageUtils<>(iPage);
    }

    @Override
    public synchronized void save(CreateOraAppVo createOraAppVo) {
        // 1.字段为UNI，需要不重复
        UserDetails currentUser = SecurityUtils.getCurrentUser();
        createOraAppVo.setCreateBy(currentUser.getUsername());
        // 2.映射数据
        OraApp oraApp = oraAppMapstruct.toOraApp(createOraAppVo);
        oraApp.setCreateTime(new Timestamp(new Date().getTime()));
        // 3.保存    
        this.save(oraApp);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public synchronized void edit(ValidList<UpdateOraAppVo> updateOraAppVoList, List<BaseErrDto> errDtoList) {
        List<OraApp> toUpdateOraAppList = new ArrayList<>();

        for (UpdateOraAppVo updateOraAppVo : updateOraAppVoList) {
            Long id = updateOraAppVo.getId();
            // 1.字段为UNI，需要不重复
            UserDetails currentUser = SecurityUtils.getCurrentUser();
            updateOraAppVo.setUpdateBy(currentUser.getUsername());
            OraApp oraApp = oraAppMapstruct.toOraApp(updateOraAppVo);
            oraApp.setUpdateTime(new Timestamp(new Date().getTime()));
            toUpdateOraAppList.add(oraApp);
        }

        // 2.更新
        this.updateBatchById(toUpdateOraAppList);
    }

    @Override
    public void remove(Set<Long> ids) {
        this.removeByIds(ids);
    }

    @Override
    public void download(HttpServletResponse response) {
        ExcelUtils.export(response,"应用管理信息表",this.list(), OraApp.class);
    }

}